#include "dlgcarpara.h"
#include "ui_dlgcarpara.h"
#include "../mainwindow.h"


#include <yaml-cpp/yaml.h>
#include <fstream>
#include <iostream>

DlgCarPara::DlgCarPara(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::DlgCarPara)
{
    ui->setupUi(this);
    slot_loadAmrYAML();
}

DlgCarPara::~DlgCarPara()
{
    delete ui;
}

void DlgCarPara::slot_loadAmrYAML()
{
    try {
        YAML::Node config = YAML::LoadFile(g_LocalMapPath.toStdString() +"/PLNS.yaml");
        ui->lineEdit_amrID->setText(QString::fromStdString(config["amr_ID"].as<std::string>()));
        ui->dSBox_width->setValue(config["amr_base_width"].as<double>());
        ui->dSBox_carlen->setValue(config["amr_base_length"].as<double>());
        ui->dSBox_radius->setValue(config["amr_base_radius"].as<double>());
        if(config["amr_shape"].as<std::string>() == "circle")
        {
            ui->radioButton_circle->setChecked(true);
            if(config["amr_base_radius"].as<double>() > 0)
                g_RobotChassisRadius = config["amr_base_radius"].as<double>();
            g_RobotChassisLength = 0;
            g_RobotChassisWidth = 0;
        }
        else if(config["amr_shape"].as<std::string>() == "rectangle")
        {
            ui->radioButton_rectangle->setChecked(true);
            if(config["amr_base_length"].as<double>() > 0)
                g_RobotChassisLength = config["amr_base_length"].as<double>();
            if(config["amr_base_width"].as<double>() > 0)
                g_RobotChassisWidth = config["amr_base_width"].as<double>();
            g_RobotChassisRadius = 0;
        }




        qDebug()<<"g_RobotChassisLength = "<<g_RobotChassisLength<<",g_RobotChassisWidth = "<<g_RobotChassisWidth <<g_RobotChassisRadius;

        ui->dSBox_lunzhijing->setValue(config["amr_wheel_diam"].as<double>());
        ui->dSBox_lunjianju->setValue(config["amr_wheel_base"].as<double>());
        ui->dSBox_jainsubi->setValue(config["amr_wheel_reduction"].as<double>());
        ui->dSBox_fenbianlv->setValue(config["amr_whell_resolition"].as<double>());
        ui->comboBox_leftZFZ->setCurrentIndex(config["motor_driver"]["amr_whell_leftReversible"].as<double>());
        ui->comboBox_rightZFZ->setCurrentIndex(config["motor_driver"]["amr_whell_rightReversible"].as<double>());
    } catch (const std::exception& e) {
        qDebug()<< "处理YAML文件时发生错误: " << e.what();
    }
}

void DlgCarPara::setParent(void *parent)
{
    m_parent = parent;
}

void DlgCarPara::on_pushButton_SetCarPare_clicked()
{
    MainWindow *parent = (MainWindow*)m_parent;
    QMessageBox::StandardButton result = QMessageBox::warning(this,QString(u8"Warning消息框"),QString(u8"是否修改car 参数并推送"),
                                                              QMessageBox::Ok|QMessageBox::Discard,
                                                              QMessageBox::Ok);
    if(result == QMessageBox::Ok)
    {
        try {
            // 加载YAML文件
            YAML::Node config = YAML::LoadFile(g_LocalMapPath.toStdString() +"/PLNS.yaml");
            // 修改YAML内容
            config["amr_ID"] = ui->lineEdit_amrID->text().toStdString();
            if(ui->radioButton_circle->isChecked())
            {
                config["amr_shape"] = "circle";
                g_RobotChassisLength = 0;
                g_RobotChassisWidth = 0;
                g_RobotChassisRadius = ui->dSBox_radius->value();
            }
            else if(ui->radioButton_rectangle->isChecked())
            {
                config["amr_shape"] = "rectangle";
                g_RobotChassisLength = ui->dSBox_carlen->value();
                g_RobotChassisWidth = ui->dSBox_width->value();
                g_RobotChassisRadius = 0;
            }
            config["amr_base_width"] = ui->dSBox_width->text().toStdString();
            config["amr_base_length"] = ui->dSBox_carlen->text().toStdString();
            config["amr_base_radius"] = ui->dSBox_radius->text().toStdString();
            config["amr_base_length"] = ui->dSBox_carlen->text().toStdString();
            config["amr_wheel_diam"] = ui->dSBox_lunzhijing->text().toStdString();
            config["amr_wheel_base"] = ui->dSBox_lunjianju->text().toStdString();
            config["amr_wheel_reduction"] = ui->dSBox_jainsubi->text().toStdString();
            config["amr_whell_resolition"] = ui->dSBox_fenbianlv->text().toStdString();
            config["motor_driver"]["amr_whell_leftReversible"] = ui->comboBox_leftZFZ->currentIndex();
            config["motor_driver"]["amr_whell_rightReversible"] = ui->comboBox_rightZFZ->currentIndex();
            // 保存修改到YAML文件
            std::ofstream fout(g_LocalMapPath.toStdString() +"/PLNS.yaml");
            fout << config;
            fout.close();
            parent->m_TMSSHConnectWGT->Public_ssh_PushFilePath("/PLNS.yaml");
            slot_loadAmrYAML();
        } catch (const std::exception& e) {
            qDebug()<< "处理YAML文件时发生错误: " << e.what();
        }
    }
    else
    {
        slot_loadAmrYAML();
    }
}


void DlgCarPara::on_radioButton_rectangle_toggled(bool checked)
{
    ui->dSBox_width->setEnabled(checked);
    ui->dSBox_carlen->setEnabled(checked);
    ui->dSBox_radius->setEnabled(!checked);
}


void DlgCarPara::on_radioButton_circle_toggled(bool checked)
{
}

